#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QObject>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    qRegisterMetaType<QList<QString>>();

    // 设置窗口固定大小为800x600
    this->setFixedSize(800, 600);
    ui->server_display->setReadOnly(true);
    ui->server_display-> setOpenExternalLinks(false);
    ui->server_display-> setOpenLinks(false);

    connection = new ConnectionDialog();

    signal_slot_init();

}

MainWindow::~MainWindow()
{
    delete ui;
    delete connection;
}

void MainWindow::signal_slot_init()
{
    connect(connection, SIGNAL(setLogs(const QString &)),
           this, SLOT(logs_slot(const QString &)),
            Qt::QueuedConnection);

    connect(this, SIGNAL(maindown_logs(const QString &)),
            this, SLOT(logs_slot(const QString &)),
            Qt::QueuedConnection);

    connect(this, SIGNAL(upload_signal(const QString &)),
            connection, SLOT(upload_slot(const QString &)),
            Qt::QueuedConnection);

    connect(ui->pushButton_7, SIGNAL(clicked()),
            connection, SLOT(refresh_slot()),
            Qt::QueuedConnection);

    connect(connection, SIGNAL(enum_down(QList<QString>, int)),
            this, SLOT(enum_down_slot(QList<QString>, int)),
            Qt::QueuedConnection);

    connect(ui->server_display, SIGNAL(anchorClicked(const QUrl &)),
            this, SLOT(select_file_slot(const QUrl &)),
            Qt::QueuedConnection);

    connect(this, SIGNAL(delete_file_signal(QString)),
            connection, SLOT(delete_cmd_slot(QString)),
            Qt::QueuedConnection);

    connect(this, SIGNAL(download_file_signal(QString)),
            connection, SLOT(download_file_slot(QString)),
            Qt::QueuedConnection);


}

void MainWindow::on_action_3_triggered()
{
    connection->exec();
}

void MainWindow::logs_slot(const QString &logs)
{
    ui->log_display->append(logs);
}



void MainWindow::on_pushButton_6_clicked()
{
    QString directory = QFileDialog::getOpenFileName(
        this,
        tr("选择本地文件"),
        "", // 可以指定一个默认路径
        tr("All Files (*.*);;Text Files (*.txt)")
    );

    // 检查用户是否选择了路径
    if (!directory.isEmpty()) {
        // 创建 QFileInfo 对象
        QFileInfo fileInfo(directory);

        if (fileInfo.isFile()) {
            ui->textEdit->setText(directory);
            qDebug() << "It is a file.";
            emit maindown_logs("选择了文件:" + fileInfo.fileName());
        }
        else {
            qDebug() << "It is not a file.";
            emit maindown_logs("选择的不是文件");
        }
    }
}



void MainWindow::on_pushButton_clicked()
{
    //上传
    QString file_path = ui->textEdit->toPlainText();
    QFileInfo fileInfo(file_path);
    if (fileInfo.exists()) {
        emit maindown_logs("开始上传文件：" + fileInfo.fileName());
        emit upload_signal(file_path);
    } else {
        emit maindown_logs("文件：" + fileInfo.fileName() + " 不存在");
    }
}


void MainWindow::addQStringToTextBrowser(QList<QString> list) {
    // 创建一个超链接
//    for (QString &fileName : list) {
    QString fileName;
    qDebug()<<list.size();
    for (int i = 0; i < list.size(); i++) {
        fileName = list[i];
        qDebug()<<"file name: "<<fileName;
        QTextCursor cursor = ui->server_display->textCursor();
        QTextCharFormat format;
        format.setAnchor(true);
        // 使用自定义的URL前缀来区分普通链接和我们的自定义链接
        QString url = QString("mylink://%1").arg(fileName);
        format.setAnchorHref(url);
        cursor.insertText(fileName, format);
        // 添加换行符以分隔字符串
        cursor.insertBlock();
        ui->server_display->setTextCursor(cursor); // 更新 QTextBrowser 的光标
    }
}

void MainWindow::enum_down_slot(QList<QString> list, int number)
{
    qDebug()<<"枚举到"<<number<<"个文件\n";
    ui->server_display->clear();
    addQStringToTextBrowser(list);
}

void MainWindow::select_file_slot(const QUrl &url)
{
    // 当文件名被点击时，触发此槽函数
    if (url.scheme() == "mylink") {
        // 提取QString，跳过前缀"mylink://"
        QString clickedString = url.toString().mid(9);
        // 显示在label上
        ui->textEdit_2->setText(clickedString);
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    QString file_name = ui->textEdit_2->toPlainText();
    if (file_name.isEmpty()) {
        emit maindown_logs("没有选中文件");
    }
    else {
        emit delete_file_signal(file_name);
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    QString file_name = ui->textEdit_2->toPlainText();
    if (file_name.isEmpty()) {
        emit maindown_logs("没有选中文件");
    }
    else {
        emit download_file_signal(file_name);
    }
}

